package com.pdd.controller;

import com.pdd.beans.Admin;
import com.pdd.dto.DTO;
import com.pdd.service.AdminService;
import com.pdd.util.EncryptUtil;
import com.pdd.util.IPUtil;
import com.pdd.vo.admin.AdminLoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * @创建时间: 2018/8/16 - 9:45
 * @编写人员: 丷教皇
 * @文件描述:
 */
@Controller
@RequestMapping("/admin")
@Api(description = "管理员相关接口")
public class AdminController {

    @Autowired
    @Setter
    private AdminService adminService;

    @ApiOperation(value = "登录验证")
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    @ResponseBody
    public DTO login(@RequestBody AdminLoginVO vo, HttpServletRequest request){


        //1.验证邮箱是否存在
        Admin result = adminService.getByEmail(vo.getA_email());
        if(result == null){
            return new DTO(2001,"邮箱账号不存在");
        }

        //2.验证密码是否正确
        String pwd = EncryptUtil.getMD5(vo.getA_pwd());
        if(!result.getA_pwd().equals(pwd)){
            return new DTO(2002,"密码错误");
        }

        //3.后续处理
        Admin admin = new Admin();
        admin.setA_email(result.getA_email());  //邮箱
        admin.setA_last_login_time(new Date()); //登录时间
        admin.setA_last_login_ip(IPUtil.getRealIP(request));
        adminService.updAdmin(admin);

        return new DTO<Admin>(1000,"登录成功",result);
    }

}
